<?php
namespace Hive\Contact\Import;
/**
 * Import File Format
 *
 * It represant a format of import file, to import contact.
 *
 * @author Xavier VILAIN
 * @package Hive\Contact
 *
 * @property int	$format		Format id
 * @property string	$language	Language
 * @property string	$label		Label
 */
class Format extends \Fox\Object\Versioned {

	/**
	 * Link to Translation
	 * @var string
	 */
	const LINK_I18N = 'i18n';

	/**
	 * Link to items
	 * @var string
	 */
	const LINK_ITEMS = 'items';

	/**
	 * 
	 * Result Grid Data
	 * @var array
	 */
	public $data = array();
	
	/**
	 * 
	 * Result Upload References 
	 * @var array
	 */
	public $references = array();
	
	/**
	 * 
	 * Result Upload Errors
	 * @var array
	 */
	public $errors = array();
		
	/**
	 * Return Title
	 * @return string Element Title
	 */
	public function label() {
		return $this->i18nParse('label');
	}
	
	/**
	 * Process i18n to get the translated value if it exist,
	 * else the system will default to page value
	 * @param string $fieldName
	 * @return string
	 */
	public function i18nParse($fieldName) {
		$language = \Xview::instance()->language();
		$defaultLanguage = $this->language;
		if($language != $defaultLanguage) {
			$i18n = $this->findI18n($this->format.':'.$language);
			if($i18n->exists()) {
				return $i18n->$fieldName;
			}
			elseif(!empty($defaultLanguage)) {
				return null;
			}
		}
		return $this->$fieldName;
	}
	
	/**
	 * Build and return output format grid
	 * 
	 * @return \Fox\Grid
	 */
	public function outputGrid() {
		$grid = new \Fox\Grid('ouptput');
		foreach($this->items() as $field) {
			/* @var $field   Hive\Contact\Import\Format\Item */
			$iname = $field->attribute;
			$idelm = $field->dataelement;
			$idelm = empty($idelm) ? $iname : $iname;
			$fields[] = array($idelm,$iname);
		}
		if(!empty($fields)) {
			$grid->fieldcat($fields);
		}
		return $grid;
	}
	
	/**
	 * 
	 * Read uploaded file according to current format
	 * @param string $filename
	 */
	public function readFile($filename) {
		if(preg_match('/xls/',$filename)) {
			$result = Format\Reader\SpreadSheet::read($filename, $this);
		} elseif(preg_match('/csv/',$filename)) {
			$result = Format\Reader\Csv::read($filename, $this);
		} else {
			return null;
		}
		$this->data = $result['data'];
		$this->references = $result['references'];
		$this->errors = $result['errors'];
		return $this->data;
	}
}
\Fox\Dao::infos(Format::className(),new \Fox\Object\Info('contact_import_formats', \Fox\Databases::DB_APP));

// Link to i18n elements
\Fox\Object\Links::add(\Fox\Dao::link(Format\I18n::className(),Format\I18n::LINK_FORMAT)
->to(Format::className(), Format::LINK_I18N)
->on('format')
->cache(new \Fox\Dao\MemoryCacheZone(\Fox\Dao\MemoryCacheZone::PERIOD_FOREVER,Format::cacheDirectory()))
->versionFrom());

// Link to fields items
\Fox\Object\Links::add(\Fox\Dao::link(Format\Item::className(),Format\Item::LINK_FORMAT)
->to(Format::className(), Format::LINK_ITEMS)
->on('format')
->orderBy('col_number')
->versionFrom());
